﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SHomeWorkshop.LunarPresentation
{

    /// <summary>
    /// 纸张信息。由物理纸张尺寸转化为WPF纸张尺寸（96DPI下）。
    /// </summary>
    public class PaperInfo
    {
        /// <summary>
        /// 构造方法。
        /// </summary>
        /// <param name="name">纸张类型名称。例如：A4</param>
        /// <param name="shortSideWPFUnit">会自动取“shortSideWPFUnit”和“longSideWPFUnit”这两个参数中较小的值来初始化“ShortSideWPFUnit”属性。</param>
        /// <param name="longSideWPFUnit">会自动取“shortSideWPFUnit”和“longSideWPFUnit”这两个参数中较大的值来初始化“LongSideWPFUnit”属性。</param>
        /// <param name="shortSidePixcelCount">与前两个参数类似，后两个参数也会自动比较大小。</param>
        /// <param name="longSidePixcelCount">与前两个参数类似，后两个参数也会自动比较大小。</param>
        public PaperInfo(string name, double shortSideWPFUnit, double longSideWPFUnit,
            int shortSidePixcelCount, int longSidePixcelCount)
        {
            paperTypeName = name;

            if (shortSideWPFUnit < longSideWPFUnit)
            {
                this.shortSideWPFUnit = shortSideWPFUnit;
                this.longSideWPFUnit = longSideWPFUnit;
            }
            else
            {
                this.shortSideWPFUnit = longSideWPFUnit;
                this.longSideWPFUnit = shortSideWPFUnit;
            }

            if (shortSidePixcelCount < longSidePixcelCount)
            {
                this.shortSidePixcelCount = shortSidePixcelCount;
                this.longSidePixcelCount = longSidePixcelCount;
            }
            else
            {
                this.shortSidePixcelCount = longSidePixcelCount;
                this.longSidePixcelCount = shortSidePixcelCount;
            }
        }

        private static List<PaperInfo> paperInfos = new List<PaperInfo>();

        static PaperInfo()
        {
            paperInfos.Add(new PaperInfo("A0", 4493.9, 3178.6, 14043, 9933));
            paperInfos.Add(new PaperInfo("A1", 3178.6, 2245.0, 9933, 7015));
            paperInfos.Add(new PaperInfo("A2", 2245.0, 1587.4, 7015, 4960));
            paperInfos.Add(new PaperInfo("A3", 1587.4, 1122.5, 4960, 3507));
            paperInfos.Add(new PaperInfo("A4", 793.7, 1122.5, 2480, 3507));
            paperInfos.Add(new PaperInfo("A5", 559.4, 793.7, 1748, 2480));
            paperInfos.Add(new PaperInfo("A6", 396.9, 559.4, 1240, 1748));
            paperInfos.Add(new PaperInfo("A7", 279.7, 396.9, 874, 1240));
            paperInfos.Add(new PaperInfo("A8", 196.5, 279.7, 614, 874));
            paperInfos.Add(new PaperInfo("A9", 139.8, 196.5, 437, 614));
            paperInfos.Add(new PaperInfo("A10", 98.3, 139.8, 307, 437));

            paperInfos.Add(new PaperInfo("B0", 3779.5, 5344.3, 11811, 16700));
            paperInfos.Add(new PaperInfo("B1", 2672.1, 3779.5, 8350, 11811));
            paperInfos.Add(new PaperInfo("B2", 1889.8, 2672.1, 5905, 8350));
            paperInfos.Add(new PaperInfo("B3", 1334.2, 1889.8, 4169, 5905));
            paperInfos.Add(new PaperInfo("B4", 944.9, 1334.2, 2952, 4169));
            paperInfos.Add(new PaperInfo("B5", 665.2, 944.9, 2078, 2952));
            paperInfos.Add(new PaperInfo("B6", 472.4, 665.2, 1476, 2078));
            paperInfos.Add(new PaperInfo("B7", 332.6, 472.4, 1039, 1476));
            paperInfos.Add(new PaperInfo("B8", 234.3, 332.6, 732, 1039));
            paperInfos.Add(new PaperInfo("B9", 166.3, 234.3, 519, 732));
            paperInfos.Add(new PaperInfo("B10", 117.2, 166.3, 366, 519));
        }

        public static PaperInfo GetPaperInfo(string paperName)
        {
            if (paperName == null) return null;

            foreach (PaperInfo pi in paperInfos)
            {
                if (pi.PaperTypeName == paperName) return pi;
            }

            return null;
        }

        //如何计算WpfPixcelUnit（Wpf与设备无关单位）值。
        //Pixcel=毫米数*（像素/毫米）
        //WpfDPI值是96。
        //WpfPixcelUnit=Pixcel*WpfDPI/ImageDPI
        //于是：
        //WpfPixcelUnit=毫米数*（像素/毫米）*WpfDPI/ImageDPI
        //对于300DPI的图片来说：1毫米＝11.811024003348像素。
        //因此：
        //WpfPixcelUnit=毫米数*11.811024003348*WpfDPI/ImageDPI
        //最终结果：
        //WpfPixcelUnit=毫米数*11.811024003348*96/300;
        //也就是：
        //WpfPixcelUnit=毫米数*3.77952768107136

        public static readonly double ratioValueFromMmToWpfPixcelUnits = 3.77952768107136;

        private string paperTypeName = "A4";

        public string PaperTypeName
        {
            get { return paperTypeName; }
        }

        private double shortSideWPFUnit = 793.7;

        public double ShortSideWPFUnit
        {
            get { return shortSideWPFUnit; }
        }

        private double longSideWPFUnit = 1122.5;

        public double LongSideWPFUnit
        {
            get { return longSideWPFUnit; }
        }

        private int shortSidePixcelCount = 2480;

        public int ShortSidePixcelCount
        {
            get { return shortSidePixcelCount; }
            set { shortSidePixcelCount = value; }
        }

        private int longSidePixcelCount = 3507;

        public int LongSidePixcelCount
        {
            get { return longSidePixcelCount; }
            set { longSidePixcelCount = value; }
        }
    }
}
